<template>
  <div class="chat-input">
    <div class="input-wrapper">
      <textarea
        ref="inputRef"
        v-model="input"
        placeholder="输入消息..."
        @keydown.enter.exact.prevent="sendMessage"
        @keydown.enter.shift.exact="newline"
        @input="adjustHeight"
        rows="1"
      ></textarea>
      <button 
        class="send-button" 
        @click="sendMessage"
        :disabled="!input.trim() || loading"
      >
        发送
      </button>
    </div>
    <div class="hint">
      按 Enter 发送，Shift + Enter 换行
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { storeToRefs } from 'pinia'
import { useChatStore } from '../stores/chat'

const chatStore = useChatStore()
const { loading } = storeToRefs(chatStore)
const input = ref('')
const inputRef = ref(null)

const sendMessage = async () => {
  const message = input.value.trim()
  if (message && !loading.value) {
    input.value = ''
    await chatStore.sendMessage(message)
    // 重置输入框高度
    if (inputRef.value) {
      inputRef.value.style.height = 'auto'
    }
  }
}

const newline = () => {
  input.value += '\n'
}

const adjustHeight = () => {
  const textarea = inputRef.value
  if (textarea) {
    textarea.style.height = 'auto'
    textarea.style.height = textarea.scrollHeight + 'px'
  }
}
</script>

<style scoped>
.chat-input {
  padding: 20px;
  background: white;
  border-top: 1px solid #e5e7eb;
  max-width: 900px;
  margin: 0 auto;
  width: 100%;
}

.input-wrapper {
  display: flex;
  gap: 10px;
  background: #f8f5f1; /* 米白色背景 */
  border: 1px solid #e5e7eb;
  border-radius: 8px;
  padding: 8px;
}

textarea {
  flex: 1;
  min-height: 24px;
  max-height: 200px;
  padding: 8px 12px;
  border: none;
  background: transparent;
  font-size: 14px;
  line-height: 1.5;
  resize: none;
  outline: none;
  font-family: inherit;
}

.send-button {
  align-self: flex-end;
  padding: 8px 16px;
  background: #059669;
  color: white;
  border: none;
  border-radius: 6px;
  font-size: 14px;
  cursor: pointer;
  transition: all 0.2s ease;
}

.send-button:hover:not(:disabled) {
  background: #047857;
}

.send-button:active:not(:disabled) {
  transform: scale(0.98);
}

.send-button:disabled {
  background: #9ca3af;
  cursor: not-allowed;
}

.hint {
  text-align: center;
  color: #6b7280;
  font-size: 12px;
  margin-top: 8px;
}

/* 暗色主题适配 */
@media (prefers-color-scheme: dark) {
  .chat-input {
    background: #1f2937;
    border-top-color: #374151;
  }

  .input-wrapper {
    background: #374151;
    border-color: #4b5563;
  }

  textarea {
    color: #e5e7eb;
  }

  textarea::placeholder {
    color: #9ca3af;
  }

  .hint {
    color: #9ca3af;
  }
}
</style> 